package com.vegetable.modules.service.bank.convert;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.StrUtil;
import com.vegetable.modules.entity.bank.vo.WechatPayTran;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 农行流水标准化字段输出
 */
@Slf4j
public class WeiChatConverter {

	public List<WechatPayTran> convert(MultipartFile file) throws Exception {
		try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
			Sheet sheet = workbook.getSheetAt(0);
			Row headRow = sheet.getRow(16);
			Short cellNum = headRow.getLastCellNum();
			Field[] fields = WechatPayTran.class.getDeclaredFields();
			List<Field> cFields = Arrays.stream(fields).filter(el -> el.getAnnotation(Excel.class) != null).collect(Collectors.toList());
			for(int i = 0; i < cellNum; i ++){
				String cellName = headRow.getCell(i).getStringCellValue();
				String fieldName = cFields.get(i).getAnnotation(Excel.class).name();
				if(StrUtil.equals(cellName, fieldName)){
					log.info("格式是对的.");
				}else{
					throw new RuntimeException("格式不对了！！！");
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		ImportParams importParams = new ImportParams();
		importParams.setNeedVerify(true);
		importParams.setHeadRows(15);
		List<WechatPayTran> orderList = ExcelImportUtil.importExcel(file.getInputStream(), WechatPayTran.class, importParams);
		return orderList;
	}
}
